package com.wu.findpath;

import javax.xml.transform.Source;
import java.util.ArrayList;

/**
 * 二叉树中和为某一值的路径
 */
class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }
}

public class Solution {
    private static ArrayList<ArrayList<Integer>> resultList = new ArrayList();
    private static ArrayList<Integer> list = new ArrayList();

    public static ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {
        if (root == null) {
            return resultList;
        }
        list.add(root.val);
        if (root.val == target && root.left == null && root.right == null) {
            resultList.add(new ArrayList<>(list));
        } else if (target > root.val) {
            if (root.left != null) {
                FindPath(root.left, target - root.val);
                list.remove(list.size() - 1);
            }
            if (root.right != null) {
                FindPath(root.right, target - root.val);
                list.remove(list.size() - 1);
            }
        }
        return resultList;
    }

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(10);
        TreeNode node2 = new TreeNode(5);
        TreeNode node3 = new TreeNode(12);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(7);
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;

        ArrayList<ArrayList<Integer>> arrayLists = FindPath(node1, 22);
        System.out.println(arrayLists);
    }
}
